﻿<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<script type="text/javascript" src="lib/jsUnit.js"></script>
<script type="text/javascript" src="../lib/html.js"></script>
<script type="text/javascript" src="../lib/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="../lib/raphael-min.js"></script>
<script type="text/javascript" src="../mote.js"></script>
<script type="text/javascript">
new JSUnit.Test("MoteJS connections").run = function(){
	this.assert(Mote.version!=null, true, "Не подключен модуль MoteJS");
}

new JSUnit.Test("Basic", "Construction").run = function(){
	var v = new Mote.Vector(2, 3);
	this.assert(v.x, 2);
	this.assert(v.y, 3);
	var v = new Mote.Vector();
	this.assert(v.x, 0);
	this.assert(v.y, 0);
}
new JSUnit.Test("From Array", "Construction").run = function(){
	var v = new Mote.Vector([2, 3]);
	this.assert(v.x, 2);
	this.assert(v.y, 3);
}
new JSUnit.Test("From Vector", "Construction").run = function(){
	var v0 = new Mote.Vector(2, 3);
	var v = new Mote.Vector(v0);
	this.assert(v.x, 2);
	this.assert(v.y, 3);
}

new JSUnit.Test("Basic 1", "Addition").run = function(){
	var v = new Mote.Vector(2, 3);
	v.Add(1, 3);
	this.assert(v.x, 3);
	this.assert(v.y, 6);
}

new JSUnit.Test("Basic 2", "Addition").run = function(){
	var v = new Mote.Vector(2, 3);
	var v1 = v.add(1, 3);
	this.assert(v.x, 2);
	this.assert(v.y, 3);
	this.assert(v1.x, 3);
	this.assert(v1.y, 6);
}

new JSUnit.Test("Basic 3", "Addition").run = function(){
	var v = new Mote.Vector(2, 3);
	var d = new Mote.Vector(1, 3);
	var v1 = v.add(d);
	this.assert(v.x, 2);
	this.assert(v.y, 3);
	this.assert(v1.x, 3);
	this.assert(v1.y, 6);
}

new JSUnit.Test("With Array", "Addition").run = function(){
	var v = new Mote.Vector(2, 3);
	v.Add([1, 3]);
	this.assert(v.x, 3);
	this.assert(v.y, 6);
}

new JSUnit.Test("With Vector", "Addition").run = function(){
	var v = new Mote.Vector(2, 3);
	v.Add(new Mote.Vector(1, 3));
	this.assert(v.x, 3);
	this.assert(v.y, 6);
}

new JSUnit.Test("Multiplication", "Simple Operations").run = function(){
	var v = new Mote.Vector(2, 3);
	v.Mul(2);
	this.assert(v.x, 4);
	this.assert(v.y, 6);
	
	var v1 = v.mul(2);
	this.assert(v.x, 4);
	this.assert(v.y, 6);
	this.assert(v1.x, 8);
	this.assert(v1.y, 12);
}

new JSUnit.Test("Scalar Product", "Simple Operations").run = function(){
	var v1 = new Mote.Vector(2, 0);
	var v2 = new Mote.Vector(0, 2);
	this.assert(Mote.Vector.scalarProd(v1, v2), 0);
	var v1 = new Mote.Vector(2, 0);
	var v2 = new Mote.Vector(3, 0);
	this.assert(Mote.Vector.scalarProd(v1, v2), 6);
}

new JSUnit.Test("Get 1", "Polar System").run = function(){
	var v = new Mote.Vector(2, 0);
	var pol = v.getPolar();
	this.assert(pol.mod, 2);
	this.assert(pol.angle, 0);
}
new JSUnit.Test("Get 2", "Polar System").run = function(){
	var v = new Mote.Vector(2, 2);
	var pol = v.getPolar();
	this.assert(pol.mod, Math.sqrt(8));
	this.assert(pol.angle, 45);
}
new JSUnit.Test("Get 3", "Polar System").run = function(){
	var v = new Mote.Vector(0, 2);
	var pol = v.getPolar();
	this.assert(pol.mod, 2);
	this.assert(pol.angle, 90);
}
new JSUnit.Test("Get 4", "Polar System").run = function(){
	var v = new Mote.Vector(0, 2);
	var pol = v.getPolar(false);
	this.assert(pol.mod, 2);
	this.assert(pol.angle, Math.PI/2);
}

new JSUnit.Test("Set 1", "Polar System").run = function(){
	var v = new Mote.Vector();
	v.SetPolar(2, 0);
	this.assert(v.x, 2, "Bad x");
	this.assert(v.y, 0, "Bad y");
}

new JSUnit.Test("Set 2", "Polar System").run = function(){
	var v = new Mote.Vector();
	v.SetPolar(2, 45);
	this.assert(Math.abs(v.x-v.y)<1e5, true, "Not equal");
}

new JSUnit.Test("Set directions", "Polar System").run = function(){
	var v = new Mote.Vector();
	v.SetPolar(2, 45); this.assert(v.x>0 && v.y>0, true, "1-st quadrant error");
	v.SetPolar(2, 135); this.assert(v.x<0 && v.y>0, true, "2-nd quadrant error");
	v.SetPolar(2, 225); this.assert(v.x<0 && v.y<0, true, "3-rd quadrant error");
	v.SetPolar(2, 315); this.assert(v.x>0 && v.y<0, true, "4-th quadrant error");
}

new JSUnit.Test("Get directions", "Polar System").run = function(){
	var v = new Mote.Vector(), pol;
	pol = v.Set(1, 1).getPolar(); this.assert(pol.angle, 45, "1-st quadrant error");
	pol = v.Set(-1, 1).getPolar(); this.assert(pol.angle, 135, "2-nd quadrant error");
	pol = v.Set(-1, -1).getPolar(); this.assert(pol.angle, -135, "3-rd quadrant error");
	pol = v.Set(1, -1).getPolar(); this.assert(pol.angle, -45, "4-th quadrant error");
}

new JSUnit.Test("Set & Get", "Polar System").run = function(){var _=this;
	function checkAngle(ang){
		var v = new Mote.Vector();
		v.SetPolar(2, ang);
		var pol = v.getPolar();
		_.assert(Math.abs(pol.mod - 2)<.01, true, "Error checking angle "+ang+". Bad mod value="+pol.mod);
		_.assert(Math.abs(pol.angle - ang)<.01, true, "Error checking angle "+ang+". Bad angle value="+pol.angle);
	}
	
	for(var i=0; i<18; i++) checkAngle(i*10);
	for(var i=0; i<-18; i--) checkAngle(i*10);
	
}

new JSUnit.Test("Test 1", "Formatting").run = function(){
	var v = new Mote.Vector(2, 3);
	this.assert(v.toString(), "(2,3)");
}

new JSUnit.Test("Test 1", "Normalization").run = function(){
	this.assert(new Mote.Vector(2, 0).Norm().toString(), "(1,0)");
	this.assert(new Mote.Vector(0, 2).Norm().toString(), "(0,1)");
}

new JSUnit.Test("Test 2", "Normalization").run = function(){
	var v = new Mote.Vector(2, 0);
	var v1 = v.norm();
	this.assert(v.toString(), "(2,0)");
	this.assert(v1.toString(), "(1,0)");
}

new JSUnit.Test("Test 3", "Normalization").run = function(){
	var v = new Mote.Vector(2, 3),
		pol = v.getPolar(),
		pol1 = v.norm().getPolar();
	
	this.assert(pol.mod!=pol1.mod, true);
	this.assert(pol1.mod, 1);
	this.assert(pol1.angle, pol.angle);
	
	v.Norm();
	this.assert(v.getPolar().angle, pol.angle);
	this.assert(v.getPolar().mod, 1);
}



function init(){
	document.getElementById("sversion").innerHTML = JSUnit.version;
	JSUnit.init();
}
</script>
</head>
<body onload="init()">
	<h1>Тесты векторной алгебры модуля MoteJS</h1>
	<div>JSUnit v.<span id="sversion"></span></div>
</body>
</html>
